﻿$(function () {
    if (localStorage.getItem('userInfo')) {
        var userInfo = JSON.parse(localStorage.getItem('userInfo'));
        if (userInfo.remeber) {
            $("#userid").val(userInfo.userName);
            $("#pwd").val(userInfo.pwd);
            $("#login_ch").attr("checked", true);
        }
    }

    $('#userid').focus(function () {
        if ($('#userid').val() != null) {
            $('#clear1').show()
        }
    });


    $('#pwd').focus(function () {
        if ($('#pwd').val() != null) {
            $('#clear2').show()
        } else {
            $('#clear2').hide()
        }
    });

    $('#clear1').click(function () {
        $('#userid').val("");
    });
    $('#clear2').click(function () {
        $('#pwd').val("");
    });

    $('#login').click(function () {
        $('.bg_login').show();
        $('.login_page').show();
    });
    $('.del').click(function () {
        $('.bg_login').hide();
        $('.login_page').hide()
    });

    $('#btn_Login').click(function () {
        if ($.trim($('#userid').val()) == '') {
            $('.prompt').html('请输入您的用户名');
            return false;
        } else if ($.trim($('#pwd').val()) == '') {
            $('.prompt').html('');
            $('.prompt1').html('请输入您的密码');
            return false;
        } else {
            var formData = {
                userName: $.trim($('#userid').val()),
                password: $("#pwd").val()
            };
            $.ajax({
                type: "POST",
                contentType: 'application/json;charset=UTF-8',
                url: "http://192.168.1.110:9010/login/login.do",
                data: JSON.stringify(formData),
                dataType: "json",
                success: function(result){
                    if (result.c == "s") {

                        result.pwd = $("#pwd").val();
                        var userInfo = result.baseUserInfoVO;
                        if ($("#login_ch").val()) {
                            userInfo.remeber = true;
                            userInfo.pwd=$("#pwd").val();
                        }
                        var orginUrl=localStorage.getItem("orginUrl");
                        localStorage.removeItem("orginUrl");
                        var orginUser=localStorage.getItem("orginUser");
                        if(orginUrl&&(orginUser==userInfo.userName)){
                            window.location = orginUrl;
                            localStorage.setItem("userInfo", JSON.stringify(userInfo));
                            return false;

                        }
                        if (userInfo.userRole == 'ims_admin') {
                            window.location = 'http://' + window.location.host + "/agx/#/advertisers";

                        } else if (userInfo.userRole == "advertiser_admin") {
                            window.location = 'http://' + window.location.host + "/agx/#/adv-data-statistics";

                        } else {
                            window.location = 'http://' + window.location.host + "/agx/#/channel-data-statistics";
                        }

                        localStorage.setItem("userInfo", JSON.stringify(userInfo));

                    } else {
                        $('.prompt1').html(result.i)
                    }
                }
            });
        }
    });


})
